package com.tju.resource.oauth.controller.tb;

import com.tju.resource.model.TbMaterial;
import com.tju.resource.server.TbMaterialService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class TbMaterialController {

    private static final String prefix = "matl";

    @Autowired
    TbMaterialService service;

    @PostMapping(prefix + "/getAll")
    @PreAuthorize("hasAuthority('sys:tool:matl:list')")
    public Result<Object> getAll(@RequestBody Map<String, String> param) {
        return service.getAll(param);
    }

    @PostMapping(prefix + "/byName")
    @PreAuthorize("hasAuthority('sys:tool:matl:list')")
    public Result<Object> byName(@RequestBody Map<String, String> params) {
        return service.byName(params);
    }

    @PostMapping(prefix + "/add")
    @PreAuthorize("hasAuthority('sys:tool:matl:add')")
    public Result<Object> add(@RequestBody TbMaterial material) {
        if (null != service.byNo(material.getMatlNumber())) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "物料编号不可重复!");
        } else {
            return service.add(material);
        }
    }

    @PostMapping(prefix + "/upd")
    @PreAuthorize("hasAuthority('sys:tool:matl:edit')")
    public Result<Object> upd(@RequestBody TbMaterial material) {
        TbMaterial matl = service.byId(material.getId());
        int updRes;

        if (null != matl) {
            matl.setSid(material.getSid());

            if (!StringUtils.equals(material.getMatlNumber(), matl.getMatlNumber())) {
                if (null == service.byNo(material.getMatlNumber())) {
                    updRes = service.upd(material);
                } else {
                    return ResultUtil.error(ResultEnum.UNKONW_ERROR, "物料编号不可重复!");
                }
            } else {
                updRes = service.upd(material);
            }
            if (updRes > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "更新物料完成!");
            } else {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "更新失败!");
            }
        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "对应物料不存在!");
        }
    }

    @PostMapping(prefix + "/del")
    @PreAuthorize("hasAuthority('sys:tool:matl:delete')")
    public Result<Object> del(@RequestBody Map<String, String> params) {
        TbMaterial material = service.byId(Long.parseLong(params.get("id")));

        if (null != material) {
            int delRes = service.del(material.getId());

            if (delRes > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "物料删除成功!");
            } else {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "删除失败!");
            }
        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "对应物料不存在!");
        }
    }

    @PostMapping(prefix + "/byOrderNumber")
    @PreAuthorize("hasAuthority('sys:tool:matl:list')")
    public Result<Object> byOrderNumber(@RequestBody Map<String, String> params) {
        return service.byOrderNumber(params);
    }
    @PostMapping(prefix + "/byOrderMatlNumber")
    @PreAuthorize("hasAuthority('sys:tool:matl:list')")
    public Result<Object> byOrderMatlNumber(@RequestBody Map<String, String> params) {
        return service.byOrderMatlNumber(params);
    }

}
